package com.shr.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shr.lease.common.exception.LeaseException;
import com.shr.lease.common.result.ResultCodeEnum;
import com.shr.lease.model.entity.*;
import com.shr.lease.model.enums.ItemType;
import com.shr.lease.web.admin.mapper.*;
import com.shr.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shr.lease.web.admin.vo.attr.AttrValueVo;
import com.shr.lease.web.admin.vo.graph.GraphVo;
import com.shr.lease.web.admin.vo.room.RoomDetailVo;
import com.shr.lease.web.admin.vo.room.RoomItemVo;
import com.shr.lease.web.admin.vo.room.RoomQueryVo;
import com.shr.lease.web.admin.vo.room.RoomSubmitVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private GraphInfoService graphInfoService;
    @Resource
    private RoomAttrValueService roomAttrValueService;
    @Resource
    private RoomFacilityService roomFacilityService;
    @Resource
    private RoomLabelService roomLabelService;
    @Resource
    private RoomPaymentTypeService roomPaymentTypeService;
    @Resource
    private RoomLeaseTermService roomLeaseTermService;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {

        //判断具体是更新还是保存房间信息
        Boolean IsUpdate = roomSubmitVo.getId() != null;
        //保存或修改房间的基本信息
        this.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();

        if (IsUpdate) {
            //批量房间的图片信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, roomId)
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
            );
            //删除房间的属性信息
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                    .eq(RoomAttrValue::getRoomId, roomId)
            );
            //删除房间的配套信息
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                    .eq(RoomFacility::getRoomId, roomId)
            );
            //删除房间标签信息
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>()
                    .eq(RoomLabel::getRoomId, roomId)
            );
            //删除房间的支付方式信息
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                    .eq(RoomPaymentType::getRoomId, roomId)
            );
            //删除房间租约信息
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                    .eq(RoomLeaseTerm::getRoomId, roomId)
            );
        }

        //插入房间图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (graphVoList != null && !graphVoList.isEmpty()) {
            List<GraphInfo> graphInfos = graphVoList.stream().map(
                    graphVo -> GraphInfo.builder()
                            .itemId(roomId)
                            .itemType(ItemType.ROOM)
                            .name(graphVo.getName())
                            .url(graphVo.getUrl())
                            .build()

            ).toList();
            graphInfoService.saveBatch(graphInfos);
        }
        //插入房间属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (attrValueIds != null && !attrValueIds.isEmpty()) {
            List<RoomAttrValue> roomAttrValues = attrValueIds.stream().map(
                            attrValueId -> RoomAttrValue.builder()
                                    .roomId(roomId)
                                    .attrValueId(attrValueId)
                                    .build())
                    .toList();
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //插入房间配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (facilityInfoIds != null && !facilityInfoIds.isEmpty()) {
            List<RoomFacility> roomFacilities = facilityInfoIds.stream().map(
                            facilityInfoId -> RoomFacility.builder()
                                    .roomId(roomId)
                                    .facilityId(facilityInfoId)
                                    .build())
                    .toList();
            roomFacilityService.saveBatch(roomFacilities);
        }
        //插入房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (labelInfoIds != null && !labelInfoIds.isEmpty()) {
            List<RoomLabel> roomLabels = labelInfoIds.stream().map(
                            labelInfoId -> RoomLabel.builder()
                                    .roomId(roomId)
                                    .labelId(labelInfoId)
                                    .build())
                    .toList();
            roomLabelService.saveBatch(roomLabels);
        }
        //插入房间支付方式信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (paymentTypeIds != null && !paymentTypeIds.isEmpty()) {
            List<RoomPaymentType> roomPaymentTypes = paymentTypeIds.stream().map(
                            paymentTypeId -> RoomPaymentType.builder()
                                    .roomId(roomId)
                                    .paymentTypeId(paymentTypeId)
                                    .build())
                    .toList();
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //插入房间可选租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (leaseTermIds != null && !leaseTermIds.isEmpty()) {
            List<RoomLeaseTerm> roomLeaseTerms = leaseTermIds.stream().map(
                    labelInfoId -> RoomLeaseTerm.builder()
                            .roomId(roomId)
                            .leaseTermId(labelInfoId)
                            .build()
            ).toList();
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }

    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //查询公寓信息 ApartmentInfo
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //查询图片列表 List<GraphVo>
//        List<GraphVo> graphVos = graphInfoMapper.selectByItemTypeAndItemId(id, ItemType.ROOM);

//        List<Map<String, Object>> maps = graphInfoMapper.selectMaps(new LambdaQueryWrapper<GraphInfo>()
//                .select(GraphInfo::getName, GraphInfo::getUrl)
//                .eq(GraphInfo::getItemId, id)
//                .eq(GraphInfo::getItemType, ItemType.ROOM));
//        maps.forEach(System.out::println);

//        List<GraphInfo> graphInfos1 = graphInfoMapper.selectList(
//                new LambdaQueryWrapper<GraphInfo>()
//                        .eq(GraphInfo::getItemId, id)
//                        .eq(GraphInfo::getItemType, ItemType.ROOM)
//        );
        List<GraphInfo> graphInfos = graphInfoService.list(
                new LambdaQueryWrapper<GraphInfo>()
                        .select(GraphInfo::getName,GraphInfo::getUrl)
                        .eq(GraphInfo::getItemType,ItemType.ROOM)
                        .eq(GraphInfo::getItemId,id)
        );
        List<GraphVo> graphVos = graphInfos.stream()
                .map(
                graphInfo -> GraphVo.builder()
                        .name(graphInfo.getName())
                        .url(graphInfo.getUrl())
                        .build())
                .toList();
        //查询属性信息列表 List<AttrValueVo>
        List<AttrValueVo> attrValueVos = roomAttrValueService.listAttrValueVo(id);
        //查询配套信息列表 List<FacilityInfo>
        List<FacilityInfo> facilityInfos = roomFacilityService.listFacilityInfo(id);
        //查询标签信息列表 List<LabelInfo>
        List<LabelInfo> labelInfos = roomLabelService.listLabelInfo(id);
        //查询支付方式列表 List<PaymentType>
        List<PaymentType> paymentTypes = roomPaymentTypeService.listPaymentType(id);
        //查询可选租期列表 List<LeaseTerm>
        List<LeaseTerm> leaseTerms = roomLeaseTermService.listLeaseTerm(id);

        RoomDetailVo roomDetailVo = RoomDetailVo.builder()
                .apartmentInfo(apartmentInfo)
                .graphVoList(graphVos)
                .attrValueVoList(attrValueVos)
                .facilityInfoList(facilityInfos)
                .labelInfoList(labelInfos)
                .paymentTypeList(paymentTypes)
                .leaseTermList(leaseTerms)
                .build();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long roomId) {

        //先判断房间下是否由租约信息
        List<Map<String, Object>> maps = leaseAgreementMapper.selectMaps(
                new LambdaQueryWrapper<LeaseAgreement>()
                        .select(LeaseAgreement::getId)
                        .eq(LeaseAgreement::getRoomId, roomId)
        );

        if (maps != null && !maps.isEmpty()) {
            throw new LeaseException(ResultCodeEnum.DELETE_ROOM_ERROR);
        }

        //删除房间基本信息
        roomInfoMapper.deleteById(roomId);
        //删除房间租期关系
        roomLeaseTermService.remove(
                new LambdaQueryWrapper<RoomLeaseTerm>()
                        .eq(RoomLeaseTerm::getRoomId,roomId)
        );
        //删除房间支付方式关系
        roomPaymentTypeService.remove(
                new LambdaQueryWrapper<RoomPaymentType>()
                        .eq(RoomPaymentType::getRoomId,roomId)
        );
        //删除房间属性关系
        roomAttrValueService.remove(
                new LambdaQueryWrapper<RoomAttrValue>()
                        .eq(RoomAttrValue::getRoomId,roomId)
        );
        //删除房间标签关系
        roomLabelService.remove(
                new LambdaQueryWrapper<RoomLabel>()
                        .eq(RoomLabel::getRoomId,roomId)
        );
        //删除房间配套关系
        roomFacilityService.remove(
                new LambdaQueryWrapper<RoomFacility>()
                        .eq(RoomFacility::getRoomId,roomId)
        );
        //删除图片信息
        graphInfoService.remove(
                new LambdaQueryWrapper<GraphInfo>()
                        .eq(GraphInfo::getItemType,ItemType.ROOM)
                        .eq(GraphInfo::getItemId,roomId)
        );
    }
}




